package net.admin.db.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MemberDAO {
	
	Connection con=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public MemberDAO(){
		try{
			Context initCtx=new InitialContext();
			Context envCtx=(Context)initCtx.lookup("java:comp/env");
			DataSource ds=(DataSource)envCtx.lookup("jdbc/OracleDB");
			con=ds.getConnection();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public int userCheck(String id, String pw){
		System.out.println("net.admin.db.member.MemberDAO.userCheck("+id+","+pw+")");
		String sql=null;
		int x=-1;
		
		try{
			sql="select PW from ADMIN where ID=?";
			pstmt=con.prepareStatement(sql);
			pstmt.setString(1, id);
			rs=pstmt.executeQuery();
			System.out.println("aa");
			if(rs.next()){
				System.out.println("bb");
				String memberpw=rs.getString("PW");
				System.out.println("net.admin.db.member.MemberDAO.userCheck("+id+","+pw+")"+"  db: memberpw="+memberpw);
				if(memberpw.equals(pw)){
					x=1;
				}else{
					x=0;
				}
			}else{
				x=-1;
			}
			
			return x;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(pstmt != null){ try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} }
			if(rs != null){ try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} }
			
		}
		
		return -1;
	}
	
}